﻿/******************************************************************
** 文 件 名: BD_Payment_Edit.aspx.cs
** Copyright (c) 2010-2012 Honestysoft (GuangZhou) Co.,LTD
** 创 建 人: 此代码由生成器自动生成
** 创建日期: 2012-3-15 19:35:24
** 描    述: 支付单编辑页面
** 版    本: 2.0.0.0
** 修改描述:
** 修 改 人:
** 修改日期:
******************************************************************/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Honestysoft.BaseLibrary;
using Honestysoft.BaseObject;
using Honestysoft.BaseLibrary.ExceptionHandling;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.Financing.Model;


namespace Honestysoft.Financing.WebUI.Module.Contract
{
	public partial class BD_Payment_Edit : BasePage
	{	
		protected override void Page_Load(object sender, EventArgs e)
        {
			base.Page_Load(sender, e);
            if (!IsPostBack)
            {
				BindControl();
                Bind();
            }
        }
		
		private void BindControl()
		{
			ControlHelper.BindDictionary(ddlPaymentType, "PaymentType");
			ControlHelper.BindDictionary(ddlPaymentSource, "PaymentSource");
			ControlHelper.BindEnum(ddlPaymentBuState, "PaymentBuState");//此处最好改成typeof(EnumItem.XXX)
		}

        private void Bind()
        {
			BD_Payment obj = null;
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                obj = new BaseBusinessLogic<BD_Payment>().GetEntity(Request.QueryString["ID"]);
				txtID.Text = obj.ID;
				txtContractID.Text = obj.ContractID;
				txtPaymentCode.Text = obj.PaymentCode;
				numAmount.Text = obj.Amount.ToString();
				dtpPaymentDate.Text = (obj.PaymentDate.HasValue ? obj.PaymentDate.Value.ToString("yyyy-MM-dd") : "");
				numTotalAmount.Text = obj.TotalAmount.ToString();
				numContractAmount.Text = obj.ContractAmount.ToString();
				numTotalPercent.Text = obj.TotalPercent.ToString();
				ddlPaymentType.SelectedValue = obj.PaymentType;
				ddlPaymentSource.SelectedValue = obj.PaymentSource;
				txtRemark.Text = obj.Remark;
				ddlPaymentBuState.SelectedValue = obj.PaymentBuState;
				txtCreator.Text = obj.Creator;
				dtpCreateTime.Text = obj.CreateTime.ToString("yyyy-MM-dd");
            }
        }
		
		protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                Edit();
            }
            else
            {
                Add();
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        private void Add()
        {            
            try
            {
                BD_Payment obj = GetSubmitObject();
                int ret = new BaseBusinessLogic<BD_Payment>().CreateEntity(obj);
                if (ret == 1)
                {
                    ScriptHelper.Window.AlertClose("保存成功！", true);
                }
                else
                {
                    ScriptHelper.Window.Alert("保存失败！");
                }
            }
            catch (BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("保存失败！系统发生了未知错误！");
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        private void Edit()
        {            
            try
            {
                BD_Payment obj = GetSubmitObject();
                int ret = new BaseBusinessLogic<BD_Payment>().UpdateEntity(obj);
                if (ret == 1)
                {
                    ScriptHelper.Window.AlertClose("保存成功！", true);
                }
                else
                {
                    ScriptHelper.Window.Alert("保存失败！");
                }
            }
            catch (BaseException bex)
            {
                ExceptionHelper.ExceptionDistribute(bex, false);
                ScriptHelper.Window.Alert(bex.Message);
            }
            catch (Exception ex)
            {
                ExceptionHelper.ExceptionDistribute(ex, false);
                ScriptHelper.Window.Alert("保存失败！系统发生了未知错误！");
            }
        }
		/// <summary>
        /// 得到提交对象
        /// </summary>
        /// <returns></returns>
        private BD_Payment GetSubmitObject()
        {
            BD_Payment obj = new BD_Payment();
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
				obj.ID = Request.QueryString["ID"];
            }
            else
            {
                obj.ID = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
            }
			obj.ID = txtID.Text;
			obj.ContractID = txtContractID.Text;
			obj.PaymentCode = txtPaymentCode.Text;
			if(numAmount.Text.Trim().Length > 0){obj.Amount = Convert.ToDecimal(numAmount.Text);}
			if(dtpPaymentDate.Text.Trim().Length > 0){obj.PaymentDate = Convert.ToDateTime(dtpPaymentDate.Text);}
			if(numTotalAmount.Text.Trim().Length > 0){obj.TotalAmount = Convert.ToDecimal(numTotalAmount.Text);}
			if(numContractAmount.Text.Trim().Length > 0){obj.ContractAmount = Convert.ToDecimal(numContractAmount.Text);}
			if(numTotalPercent.Text.Trim().Length > 0){obj.TotalPercent = Convert.ToDecimal(numTotalPercent.Text);}
			obj.PaymentType = ddlPaymentType.SelectedValue;
			obj.PaymentSource = ddlPaymentSource.SelectedValue;
			obj.Remark = txtRemark.Text;
			obj.PaymentBuState = ddlPaymentBuState.SelectedValue;
			obj.Creator = txtCreator.Text;
			if(dtpCreateTime.Text.Trim().Length > 0){obj.CreateTime = Convert.ToDateTime(dtpCreateTime.Text);}
            return obj;
        }
	}
}